package com.lfk.justweengine.Utils.music;

import com.lfk.justweengine.Engine.Engine;
import com.lfk.justweengine.Info.UIdefaultData;
import com.lfk.justweengine.Utils.logger.Logger;
import ohos.global.resource.ResourceManager;
import ohos.media.audio.AudioManager;
import ohos.media.audio.SoundPlayer;

import java.util.HashMap;

/**
 * SoundManager sound manger
 *
 * @author liufengkai
 *         Created by liufengkai on 16/2/5.
 */
public class SoundManager {
    private ResourceManager assetManager;
    // sound pool
    private SoundPlayer soundPool;

    // musicName musicID
    private HashMap<String, Integer> musicMap;

    public SoundManager(Engine engine, int size) {
        engine.setVolumeTypeAdjustedByKey(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
        // init
        this.assetManager = engine.getResourceManager();
        this.soundPool = new SoundPlayer(size);
        this.musicMap = new HashMap<>();
    }

    public void addSound(String musicName) {
        try {
            musicMap.put(musicName, soundPool.createSound(musicName));
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e("Couldn't found music");
        }
    }

    public void removeSound(String musicName) {
        if (musicMap.containsKey(musicName)) {
            soundPool.deleteSound(musicMap.get(musicName));
            musicMap.remove(musicName);
        } else {
            Logger.e("Couldn't found music");
        }
    }

    /**
     * play sound
     *
     * @param musicName name in assets
     * @param volume    sound's volume
     */
    public void play(String musicName, float volume) {
        if (musicMap.containsKey(musicName)) {
            soundPool.play(musicMap.get(musicName), setSoundPlayerParameters(volume));
        }
    }

    /**
     * play sound with defaultMusicVolume
     *
     * @param musicName name in assets
     */
    public void play(String musicName) {
        if (musicMap.containsKey(musicName)) {
            soundPool.play(musicMap.get(musicName), setSoundPlayerParameters(UIdefaultData.defaultMusicVolume));
        }
    }

    /**
     * play sound with musicID in SoundPool
     *
     * @param musicID musicID in SoundPool
     */
    public void play(int musicID) {
        if (musicMap.containsValue(musicID)) {
            soundPool.play(musicID, setSoundPlayerParameters(UIdefaultData.defaultMusicVolume));
        }
    }

    /**
     * play sound with musicID in SoundPool
     *
     * @param musicID musicID in SoundPool
     * @param volume  volume
     */
    public void play(int musicID, float volume) {
        if (musicMap.containsValue(musicID)) {
            soundPool.play(musicID, setSoundPlayerParameters(volume));
        }
    }

    /**
     * Is music in map?
     *
     * @param soundName soundName
     * @return Is music in map?
     */
    public boolean containSound(String soundName) {
        return musicMap.containsKey(soundName);
    }

    /**
     * Is music in map?
     *
     * @param soundID soundID
     * @return Is music in map?
     */
    public boolean containSoundID(int soundID) {
        return musicMap.containsValue(soundID);
    }

    /**
     * get Music from Name
     *
     * @param soundName soundName
     * @return soundID
     */
    public int getSoundID(String soundName) {
        return musicMap.get(soundName);
    }

    /**
     * set Music volume
     *
     * @param volume volume
     * @return soundID
     */
    private SoundPlayer.SoundPlayerParameters setSoundPlayerParameters(float volume) {
        SoundPlayer.SoundPlayerParameters soundPlayerParameters = new SoundPlayer.SoundPlayerParameters();
        SoundPlayer.AudioVolumes audioVolumes = new SoundPlayer.AudioVolumes();
        audioVolumes.setFrontLeftVolume(volume);
        audioVolumes.setFrontRightVolume(volume);
        soundPlayerParameters.setVolumes(audioVolumes);
        return soundPlayerParameters;
    }


}
